package com.wimcorp.magic.abilities;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.wimcorp.magic.abilities.trigger.AbilityTrigger;
import com.wimcorp.magic.behaviour.Behaviour;
import com.wimcorp.magic.cards.Creature;
import com.wimcorp.magic.input.BehaviorInput;
import com.wimcorp.magic.input.Target;

/**
 * example
 * {target:'OPPONENT',locations:['BATTLEFIELD'],behaviors:['DEATH_TOUCH']}
 * 
 * target options : SELF,OPPONENT,ANY
 * 
 * types options : 'CREATURE'
 * 
 * location options : BATTLEFIELD
 * 
 * behaviors options :
 * 
 * duration : END_OF_TURN,PERMANENT
 */
@Entity
@Table(name = "addbehavior", schema = "abilities")
public class AddBehavior extends Ability {

	@Transient
	private Creature creature = null;
	@Transient
	private Behaviour behaviour = null;

	@Enumerated(EnumType.STRING)
	private BehaviorInput behaviorInput;

	public AddBehavior() {
	}

	public AddBehavior(AbilityTrigger trigger) {
		super(trigger);
	}

	public AddBehavior(AddBehavior ability) {
		super(ability);
		this.behaviorInput = ability.getBehaviorInput();
	}

	protected void executeAbility() {
		creature = (Creature) getAbilityTarget();
		if (creature != null) {
			behaviour = Behaviour.getBehavior(behaviorInput);
			if (behaviorInput != null) {
				creature.addBehaviour(behaviour);
			}
			registerUndo();
		}

	}

	public void uncast() {
		if (creature != null && behaviour != null) {
			creature.removeBehaviour(behaviour);
		}
	}

	@Override
	public AddBehavior clone() {
		return new AddBehavior(this);
	}

	@Override
	public boolean isInputRequired() {
		if (Target.THIS.equals(getTarget())) {
			return false;
		}
		return true;
	}

	public BehaviorInput getBehaviorInput() {
		return behaviorInput;
	}

	public void setBehaviorInput(BehaviorInput behaviorInput) {
		this.behaviorInput = behaviorInput;
	}

}
